﻿/*
 *	Copyright (c) 2009 Queensland University of Technology. All rights reserved.
 *	The QUT Bioinformatics Collection is open source software released under the 
 *	Microsoft Public License (Ms-PL): http://www.microsoft.com/opensource/licenses.mspx.
 */
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using QUT.Bio.Util;
using QUT.Bio.SilverMap;

using WinForms = System.Windows.Forms;

namespace WpfSilverMap {
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class Window1 : Window {
		public Window1() {
			InitializeComponent();
			map.Initialize( overlay );
			map.VerticalAlignment = VerticalAlignment.Stretch;
			map.View.GraphView.Canvas.MaximumScale = 12;
			map.Model.DataProducer = new QUT.Bio.SilverMap.DefaultProvider.DataProducer( overlay );
		}

		private void LoadFromWeb ( object sender, RoutedEventArgs e ) {
			map.Model.DataProducer.DisplayLoadDialog();
		}

#if SaveEnabled
		/// <summary>
		/// Fills out the gene details and blast details from the data layer (if not null)
		/// and saves the collection.
		/// </summary>
		private void Save () {
			//statusBar.( "Downloading blast details", 300, 120 );

			map.Model.GetAllAdditionalGeneDetails( delegate() {
				map.Model.GetAllSpecificBlastDetails( delegate() {
					progress.Container = null;
					Status.WriteLine( "{0}: GetAllSpecificBlastDetails finished.", DateTime.Now );
					CompleteSaveOperation();
				} );
			} );
		}

		// TODO: Fix the Silverlight version; save-dialog if v3; hack if v2.
		private void CompleteSaveOperation () {
#if SILVERLIGHT
					/*
					OpenFileDialog dlg = new OpenFileDialog();
					dlg.Filter = "SilverMap Files (*.smml)|*.smml|Xml documents (*.xml)|*.xml|All files (*.*)|*.*";
					bool? ok = dlg.ShowDialog();

					if ( ok != null && (bool) ok ) {
						try {
							using ( StreamReader reader = dlg.File.OpenText() ) {
					*/
					if ( true ) {
						try {
							using ( StringWriter writer = new StringWriter() ) {
#else
			WinForms.SaveFileDialog dialog = new WinForms.SaveFileDialog {
				Filter = "SilverMap Files (*.smml)|*.smml|Xml documents (*.xml)|*.xml|All files (*.*)|*.*",
				OverwritePrompt = true
			};
			WinForms.DialogResult result = dialog.ShowDialog();

			if ( result == WinForms.DialogResult.OK ) {
				try {
					using ( StreamWriter writer = new StreamWriter( dialog.OpenFile() ) ) {
#endif
						map.Model.Save( writer );
					}
				}
				catch ( Exception e ) {
					overlay.Alert( e.ToString() );
				}
			}
		} 
#endif
	}
}
